package ec.atreasure.com.core.ui;

import android.content.Context;
import android.support.v7.app.AppCompatDialog;
import android.view.Gravity;
import android.view.Window;
import android.view.WindowManager;

import com.blankj.utilcode.util.ScreenUtils;
import com.wang.avi.AVLoadingIndicatorView;
import com.wang.avi.Indicator;
import com.wang.avi.indicators.BallSpinFadeLoaderIndicator;

import java.util.ArrayList;

import ec.atreasure.com.core.R;

/**
 * Description : 通用加载过度动画逻辑封装
 * Created by wangjin on 2019/6/23.
 * Job number：
 * Phone ：18301070822
 * Email： 120182051@qq.com
 * Person in charge : 汪渝栋
 * Leader：
 */
public final class AtreasureLoader {
    //dialog缩放比例
    private static final int LOADER_SIZE_SCALE = 8;

    //dialog上下偏移量
    private static final int LOADER_OFFSIZE_SCALE = 10;

    //Dialog缓存
    private static final ArrayList<AppCompatDialog> LOADERS = new ArrayList<>();

    //默认展示的过度动画
    private static final Indicator DEFAULT_LOADER = new BallSpinFadeLoaderIndicator();

    /**
     * 创建dialog
     *
     * @param context                当前展示界面的上下文
     * @param avLoadingIndicatorView 展示图标容器
     * @return
     */
    private static AppCompatDialog createDialog(Context context, AVLoadingIndicatorView avLoadingIndicatorView) {
        final AppCompatDialog dialog = new AppCompatDialog(context, R.style.dialog);
        int deviceWidth = ScreenUtils.getScreenWidth();
        int deviceHeight = ScreenUtils.getScreenHeight();

        final Window dialogWindow = dialog.getWindow();
        dialog.setContentView(avLoadingIndicatorView);
        if (dialogWindow != null) {
            final WindowManager.LayoutParams layoutParams = dialogWindow.getAttributes();
            layoutParams.width = deviceWidth / LOADER_SIZE_SCALE;
            layoutParams.height = deviceHeight / LOADER_SIZE_SCALE;
            layoutParams.height = layoutParams.height + deviceHeight / LOADER_OFFSIZE_SCALE;
            layoutParams.gravity = Gravity.CENTER;
        }

        LOADERS.add(dialog);

        return dialog;
    }

    /**
     * 显示过度动画
     *
     * @param context
     * @param type
     */
    public static void showLoading(Context context, Enum<LoaderStyle> type) {
        showLoading(context, type.name());
    }

    /**
     * 显示过度动画
     *
     * @param context
     * @param type
     */
    public static void showLoading(Context context, String type) {
        final AVLoadingIndicatorView avLoadingIndicatorView = new AVLoadingIndicatorView(context);
        avLoadingIndicatorView.setIndicator(type);
        createDialog(context, avLoadingIndicatorView).show();
    }

    /**
     * 显示过度动画
     *
     * @param context
     * @param indicator
     */
    public static void showLoading(Context context, Indicator indicator) {
        final AVLoadingIndicatorView avLoadingIndicatorView = new AVLoadingIndicatorView(context);
        avLoadingIndicatorView.setIndicator(indicator);
        createDialog(context, avLoadingIndicatorView).show();
    }

    /**
     * 显示默认过度动画
     *
     * @param context
     * @param type
     */
    public static void showLoading(Context context) {
        showLoading(context, DEFAULT_LOADER);
    }

    /**
     * 停止过度动画
     */
    public static void stopLoading() {
        for (AppCompatDialog dialog : LOADERS) {
            if (dialog != null) {
                if (dialog.isShowing())
                    dialog.cancel();
            }
        }
        LOADERS.clear();
    }
}
